package com.atguigu.gulimail.order.controller;

import com.atguigu.gulimail.order.entity.OrderEntity;
import com.atguigu.gulimail.order.entity.OrderSettingEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author fangy
 * @date 2022-10-15 11:08
 */
@Slf4j
@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/sendMsg")
    public String rabbitTest(@RequestParam(value = "num",defaultValue = "10") int num){

        for (int i = 0; i < num; i++) {
            if(i%2==0){
                OrderEntity entity = new OrderEntity();
                entity.setBillHeader("123");
                entity.setBillContent("123");

                rabbitTemplate.convertAndSend("exchange.direct","#.news",entity,new CorrelationData(UUID.randomUUID().toString()));

            }else {
                OrderSettingEntity entity = new OrderSettingEntity();
                entity.setCommentOvertime(123);
                rabbitTemplate.convertAndSend("exchange.direct","gulishagnchen.news",entity,new CorrelationData(UUID.randomUUID().toString()));
            }
            log.info("消息发送成功");
        }
        return "ok";
    }

}
